/*
 * net.c
 *
 *  Created on: 2012-8-25
 *      Author: dietoad
 */
#include "../../include/mcf.h"
#include "assert.h"
#include "stdio.h"
static void init_win_socket()
{
#ifdef _WIN32
     WSADATA data;
     WSAStartup(MAKEWORD(2,2),&data);
#endif
}
i32
service_server(socket_t *sock_t)
{
    i32 ret=0;
    linger lg;
    socket_fd sfd=0;
    sockaddr_in addr;
    assert(sock_t!=NULL);
#ifdef _WIN32
    init_win_socket();
#endif
    if(INVALID_SOCKET==(sfd=socket(AF_INET,sock_t->type,sock_t->protocl)))
    {
        mcf_error("create socket error");
        ret=-1;
    }
    sock_t->sock_fd=sfd;
    addr.sin_family=AF_INET;
    lg.l_onoff=1;
    lg.l_linger=1;
    setsockopt(sfd,SOL_SOCKET,SO_LINGER,(const i8*)&lg,1);
    if(sock_t->addr!=NULL)
    {
        addr.sin_addr.s_addr=inet_addr(sock_t->addr);
    }else{
        addr.sin_addr.s_addr=INADDR_ANY;
    }
    addr.sin_port=htons(sock_t->port);
    if(ret==0&&SOCKET_ERROR==bind(sfd,(struct sockaddr*)&addr,sizeof(addr)))
    {
        mcf_error("bind socket error");
        ret=-1;
    }
    if(ret==0&&SOCKET_ERROR==listen(sfd,sock_t->back_log))
    {
        mcf_error("listen socket error");
        ret=-1;
    }
    return ret;
}
i32
service_client(socket_t *sock_t)
{
    assert(sock_t!=NULL);
    return 0;
}
